﻿
$(document).ready(function() {
    var roottype = UrlParm.parm('roottype');
    if (roottype == "movenode") {
        moveNode();
    }
    else if (roottype == "restorenode") {
        restoreNode();
    }
    else if (roottype == 'createnodebase') {
        createNodeBase();
    }
    else if (roottype == 'doccut') {
        docCut();
    }
    else if (roottype == 'doccopy') {
        docCopy();
    }
    else if (roottype == 'createlink') {
        createLink();
    }
    else if (roottype == 'createindexlink') {
        createIndexLink();
    }
    else {
        selectroot();
    }
});
//创建虚链接
function createLink() {
    var objifram = $('#workFrame');
    var contentId = UrlParm.parm('contentid');
    var parentNodeId = parent.NodeID;
    if (contentId == undefined) { alert("参数有误"); return; }
    if (contentId == "ids") { //需要获取父窗口集合
        contentId = parent.getSelectVal();
    }
    if (objifram != undefined)
    { objifram.attr("src", 'lefttree.aspx?view=' + UrlParm.parm('view') + '&menu=' + UrlParm.parm('menu') + '&t=' + Math.random()); }
    $("#tr_root").hide();
    $("#tr_text").hide();
    $("#btok").click(function () {//确定
        //创建虚链接
        var setId = $("#hidsetid").val();
        if (setId == '' || setId == '0') { alert("请选择结点"); return false; }
        var returntext = $.ajax({ type: "POST", url: 'publish/content_saveAdvance.aspx', data: 'ac=ajax_createlink&nodeid=' + setId + '&id=' + contentId + '&oldnodeid=' + parentNodeId, async: false }).responseText;
        //alert(returntext);//调试错误信息
        var msg = "";
        try {
            msg = eval("msg=" + returntext);
        }
        catch (err) {
            msg.msg = returntext;
        }
        if (msg.result == "1") {
            alert(msg.msg);
            parent.window.location.reload(true);
        }
        else {
            alert(msg.msg); //出错信息

        }
        parent.window.location.reload(true);
    });
    $("#btreset").click(function() {//取消
        parent.close_dialog();
    });

}
//创建索引链接
function createIndexLink() {
    var objifram = $('#workFrame');
    var contentId = UrlParm.parm('contentid');
    var parentNodeId = parent.NodeID;
    if (contentId == undefined) { alert("参数有误"); return; }
    if (contentId == "ids") { //需要获取父窗口集合
        contentId = parent.getSelectVal();
    }
    if (objifram != undefined)
    { objifram.attr("src", 'lefttree.aspx?view=' + UrlParm.parm('view') + '&menu=' + UrlParm.parm('menu') + '&t=' + Math.random()); }
    $("#tr_root").hide();
    $("#tr_text").hide();
    $("#btok").click(function() {//确定
        //创建索引链接
        var setId = $("#hidsetid").val();
        if (setId == '' || setId == '0') { alert("请选择结点"); return false; }
        var returntext = $.ajax({ type: "POST", url: 'publish/content_saveAdvance.aspx', data: 'ac=ajax_createindexlink&nodeid=' + setId + '&id=' + contentId + '&oldnodeid=' + parentNodeId, async: false }).responseText;
        //alert(returntext);//调试错误信息
        //var msg = eval("msg=" + returntext);
        var msg = "";
        try {
            msg = eval("msg=" + returntext);
        }
        catch (err) {
            msg.msg = returntext;
        }
        if (msg.result == "1") {
            alert(msg.msg);
            parent.window.location.reload(true);
        }
        else {
            alert(msg.msg); //出错信息

        }
        parent.window.location.reload(true);
    });
    $("#btreset").click(function() {//取消
        parent.close_dialog();
    });
}
//内容复制
function docCopy() {

    var objifram = $('#workFrame');
    var copyId = UrlParm.parm('copycontentid');
    if (copyId == undefined) { alert("参数有误"); return; }
    if (copyId == "ids") { //需要获取父窗口集合
        copyId = parent.getSelectVal();
    }
    if (objifram != undefined)
    { objifram.attr("src", 'lefttree.aspx?view=' + UrlParm.parm('view') + '&menu=' + UrlParm.parm('menu') + '&t=' + Math.random()); }
    $("#tr_root").hide();
    $("#tr_text").hide();
    $("#btok").click(function () {//确定
        //内容复制
        var setId = $("#hidsetid").val();
        if (setId == '' || setId == '0') { alert("请选择结点"); return false; }
        var returntext = $.ajax({ type: "POST", url: 'publish/content_saveAdvance.aspx', data: 'ac=ajax_copy_doc&nodeid=' + setId + '&id=' + copyId, async: false }).responseText;
        //alert(returntext); //调试错误信息
        //var msg = eval("msg=" + returntext);
        var msg = "";
        try {
            msg = eval("msg=" + returntext);
        }
        catch (err) {
            msg.msg = returntext;
        }
        if (msg.result == "1") {
            //alert(msg.ids);
            if (msg.ids) {
                //alert(msg.ids);
                parent.top.TaskRun('publish/admin_taskanalysis.aspx?ac=PubContent&contentid=' + msg.ids + '&nodeid=' + parent.NodeID);
                //parent.window.location = 'publish/admin_publish.aspx?ac=PubContent&contentid=' + msg.ids + '&nodeid=' + parent.NodeID;
                parent.window.location.reload(true);
            }
            else {
                alert("操作成功");
                parent.window.location.reload(true);
            }
        }
        else {
            alert(msg.msg); //出错信息
            parent.window.location.reload(true);
        }

    });
    $("#btreset").click(function() {//取消
        parent.close_dialog();
    });

}
//内容剪切
function docCut() {
    var objifram = $('#workFrame');
    var cutId = UrlParm.parm('cutcontentid');
    if (cutId == undefined) { alert("参数有误"); return; }
    if (cutId == "ids") { //需要获取父窗口集合
        cutId = parent.getSelectVal();
    }
    if (objifram != undefined)
    { objifram.attr("src", 'lefttree.aspx?view=' + UrlParm.parm('view') + '&menu=' + UrlParm.parm('menu') + '&t=' + Math.random()); }
    $("#tr_root").hide();
    $("#tr_text").hide();
    $("#btok").click(function() {//确定
        //内容剪切
        var setId = $("#hidsetid").val();
        if (setId == '' || setId == '0') { alert("请选择结点"); return false; }
        var returntext = $.ajax({ type: "POST", url: 'publish/content_saveAdvance.aspx', data: 'ac=ajax_cut_doc&nodeid=' + setId + '&id=' + cutId, async: false }).responseText;
        //alert(returntext);//调试错误信息
        //var msg = eval("msg=" + returntext);
        var msg = "";
        try {
            msg = eval("msg=" + returntext);
        }
        catch (err) {
            msg.msg = returntext;
        }
        if (msg.result == "1") {
            alert("操作成功");
            parent.window.location.reload(true);
        }
        else {
            alert(msg.msg); //出错信息

        }
        parent.window.location.reload(true);
    });
    $("#btreset").click(function() {//取消
        parent.close_dialog();
    });
}

//新建结点基于
function createNodeBase() {
    var objifram = $('#workFrame');
    if (objifram != undefined)
    { objifram.attr("src", 'lefttree.aspx?view=' + UrlParm.parm('view') + '&menu=' + UrlParm.parm('menu') + '&t=' + Math.random()); }
    $("#tr_root").hide();
    $("#tr_text").hide();
    $("#btok").click(function() {//确定
        //新建结点
        var setId = $("#hidsetid").val();
        if (setId == '' || setId == '0') { alert("请选择结点"); return false; }
        top.mainwin.location = 'node/Node_save.aspx?pid=' + UrlParm.parm('pid') + '&baseid=' + setId;
        parent.close_dialog();
    });
    $("#btreset").click(function() {//取消
        parent.close_dialog();
    });
}
// 移动结点
function moveNode() {
    var objifram = $('#workFrame');
    if (objifram != undefined)
    { objifram.attr("src", 'lefttree.aspx?view=' + UrlParm.parm('view') + '&movenodeid=' + UrlParm.parm('movenodeid') + '&menu=' + UrlParm.parm('menu') + '&t=' + Math.random()); }
    $("#tr_root").hide();
    $("#tr_text").hide();
    $("#btok").click(function() {//确定
        //开始移动结点
        var setId = $("#hidsetid").val();
        if (setId == '') { alert("请选择结点"); return false; }

        top.mainwin.location = 'node/Node_save.aspx?nodeid=' + UrlParm.parm('movenodeid') + '&newpid=' + setId;
        top.doing('go_site_admin');
        parent.close_dialog();
    });
    $("#btreset").click(function() {//取消
        parent.close_dialog();
    });

}
//恢复结点到
function restoreNode() {
    var objifram = $('#workFrame');
    var restorenodeid = UrlParm.parm('restorenodeid');
    if (objifram != undefined)
    { objifram.attr("src", 'lefttree.aspx?view=' + UrlParm.parm('view') + '&menu=' + UrlParm.parm('menu') + '&t=' + Math.random()); }
    $("#tr_root").hide();
    $("#tr_text").hide();
    $("#btok").click(function() {//确定
        //开始恢复结点
        var setId = $("#hidsetid").val();
        if (setId == '') { alert("请选择结点"); return false; }
        var returntext = $.ajax({ type: "POST", url: 'node/Node_control.aspx', data: 'ac=ajax_restore&nodeid=' + setId + '&restorenodeid=' + restorenodeid, async: false }).responseText;
        //alert(returntext);
        //var msg = eval("msg=" + returntext);
        var msg = "";
        try {
            msg = eval("msg=" + returntext);
        }
        catch (err) {
            msg.msg = returntext;
        }
        if (msg.result == "1") {
            alert(msg.msg);
            top.OpenLeftMenu('tree_panel&view=site');
            if (top.mainwin.location.href.indexOf('node/Site_Recycle_Bin.aspx?nodeid=' + restorenodeid) > 0) {
                top.doing('go_sys_index');
            }
        } else {
            alert(msg.msg);
        }
        parent.close_dialog();
    });
    $("#btreset").click(function() {//取消
        parent.close_dialog();
    });

}

function setTargetNodeID(targetNodeID) {//设置要移到的结点
    //document.form1.targetNodeID.value = targetNodeID
    // alert(targetNodeID);
    $("#hidsetid").val(targetNodeID);
}

//文件选择
function selectroot() {
    var _selectpath = $("#selectpath")
    $('#hidsetid').val(UrlParm.parm('setid'));
    $('#hidselpath').val(UrlParm.parm('selpath'));
    $('#hidextname').val(UrlParm.parm('extname'));
    $('#hidroottype').val(UrlParm.parm('roottype'));
    //设置选中的路径
    _selectpath.val($('#hidselpath').val());
    var path = getRootPath(); //获取目录
    var objifram = $('#workFrame');
    if (objifram != undefined)
    { objifram.attr("src", 'AdminFileSelectWork.aspx?extname=' + $('#hidextname').val() + '&path=' + path + '&selpath=' + $("#selectpath").val()); }

    $('#ddlRoot').change(function() {
        path = getRootPath(); //获取目录
        if (objifram != undefined)
        { objifram.attr("src", 'AdminFileSelectWork.aspx?extname=' + $('#hidextname').val() + '&path=' + path + '&selpath='); }
        $("#selectpath").val('');
    });

    $("#btok").click(function() {//确定
        //向父窗口传值
        setParentValue();
        parent.close_dialog();
    });
    $("#btreset").click(function() {//取消
        parent.close_dialog();
    });
    $('#btuproot').click(function() {//上级目录

        var _currtentPaht = window.frames['workFrame'].UrlParm.parm('path');
        if (_currtentPaht.indexOf('/') >= 0 && _currtentPaht != getRootPath()) {
            var _uproot = _currtentPaht.substring(0, _currtentPaht.lastIndexOf('/'));
            objifram.attr("src", 'AdminFileSelectWork.aspx?extname=' + $('#hidextname').val() + '&path=' + _uproot);
            if (_uproot.indexOf('/') > 0) {
                $("#selectpath").val(_uproot);
            } else {
                $("#selectpath").val('');
            }
        } else {
            $("#selectpath").val('');
        }
    });
}

//获取目录
function getRootPath() {
    var rootType = $('#hidroottype').val();
    if (rootType == 'psn') {
        var _psnroot = $('#ddlRoot').val().split('|')[1];
        return _psnroot;
    }
    else {
        return $('#ddlRoot').val();
    }
}
//向父窗口传值
function setParentValue() {
    var _setid = $('#hidsetid').val();
    var _selectpath = $("#selectpath").val();
    if (_setid != '') {
        if ($('#hidroottype').val() == "psn") {
            var _psnId = $('#ddlRoot').val().split('|')[0];
            var _txtpsn = parent.$('#' + _setid + 'PSN');
            var _txtpsnurl = parent.$('#' + _setid + 'Url');
            if (_txtpsn != undefined) {
                _txtpsn.val('{PSN:' + _psnId + '}' + _selectpath);
            }
            if (_txtpsnurl != undefined) {
                _txtpsnurl.val('{PSN-URL:' + _psnId + '}' + _selectpath);
            }

        } else {
            var _parentTxt = parent.$('#' + _setid);
            var _parentRoot = parent.$('#' + _setid + 'Root');

            if (_parentRoot != undefined) {
                _parentRoot.val($('#ddlRoot').val());
            }
            _parentTxt.val(_selectpath);
        }
    }

}



